﻿#include "mainwindow.h"

#include <QFile>
#include <QFileDialog>
#include <QMenuBar>
#include <QStatusBar>
#include <QTextCodec>
#include <QVBoxLayout>
#include <QKeySequence>

#include <common/common.h>

const size_t MAX_FILE_SIZE {10*1024*1024};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    m_pToolbar = new QToolBar(this);
    m_pTextEdit = new QTextEdit(this);
    addToolBar(Qt::ToolBarArea::TopToolBarArea, m_pToolbar);
//    QVBoxLayout* pLayout = new QVBoxLayout();
//    pLayout->addWidget(m_pToolbar);
//    pLayout->addWidget(m_pTextEdit);
    setCentralWidget(m_pTextEdit);
    QStatusBar* pStatusBar = new QStatusBar(this);
    setStatusBar(pStatusBar);
    m_pCodeLabel = new QLabel(pStatusBar);
    pStatusBar->addPermanentWidget(m_pCodeLabel);
    UpdateStatusBar();

    //初始化菜单
    QMenuBar* menuBar = new QMenuBar(this);
    //文件菜单
    QMenu* pFileMenu = menuBar->addMenu(u8"文件(&N)");
    //打开
    QAction* actionOpen = pFileMenu->addAction(u8"打开(&N)");
    actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
    actionOpen->setStatusTip(u8"打开一个文件");
    actionOpen->setIcon(QIcon(u8":/new/prefix1/res/打开16.png"));
    connect(actionOpen, &QAction::triggered, this, &MainWindow::OnFileOpen);
    //保存
    QAction* actionSave = pFileMenu->addAction(u8"保存(&S)");
    actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
    actionSave->setStatusTip(u8"保存当前文件");
    actionSave->setIcon(QIcon(u8":/new/prefix1/res/保存16.png"));
    connect(actionSave, &QAction::triggered, this, &MainWindow::OnFileSave);

    QMenu* pEditMenu = menuBar->addMenu(u8"编辑(&E)");
    pEditMenu->addAction(u8"剪切(&X)", m_pTextEdit, SLOT(cut()), QKeySequence("Ctrl+X"));
    pEditMenu->addAction(u8"复制(&C)", m_pTextEdit, SLOT(copy()), QKeySequence("Ctrl+C"));
    pEditMenu->addAction(u8"粘贴(&V)", m_pTextEdit, SLOT(paste()), QKeySequence("Ctrl+V"));


    //添加工具栏图标
    m_pToolbar->addAction(actionOpen);
    m_pToolbar->addAction(actionSave);

    setMenuBar(menuBar);
}

MainWindow::~MainWindow()
{

}

void MainWindow::OnFileOpen()
{
    m_file_path = QFileDialog::getOpenFileName(this, QString(), QString(), u8"文本文件 (*.txt);;所有文件 (*.*)");
    OpenFile();
}

void MainWindow::OnFileSave()
{

}

void MainWindow::OpenFile()
{
    QFile file(m_file_path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    //读取文件内容
    m_file_contents = file.read(MAX_FILE_SIZE);
    //判断编码格式
    if(m_file_contents.size() >= 3 && m_file_contents[0]=='\xef' && m_file_contents[1]=='\xbb' && m_file_contents[0]=='\xbf')
    {
        m_code_type = CodeType::UTF8_BOM;
    }
    else if(m_file_contents.size() >= 2 && m_file_contents[0]=='\xff' && m_file_contents[1]=='\xfe')
    {
        m_code_type = CodeType::UTF16;
    }
    else if(CCommon::IsUtf8Bytes(m_file_contents))
    {
        m_code_type = CodeType::UTF8;
    }

    //更新状态栏
    UpdateStatusBar();

    //转换成QString
    if(m_code_type == CodeType::UTF8 || m_code_type == CodeType::UTF8_BOM)
    {
        m_file_string = QString::fromUtf8(m_file_contents);
    }
    else if(m_code_type == CodeType::UTF16)
    {
        m_file_string = QString::fromUtf16((const ushort*)m_file_contents.constData());
    }
    else
    {
        m_file_string = QTextCodec::codecForName("GBK")->toUnicode(m_file_contents);
    }
    //
    m_pTextEdit->setPlainText(m_file_string);
}

void MainWindow::UpdateStatusBar()
{
    QString code_text = u8"编码格式：";
    switch (m_code_type)
    {
    case CodeType::ANSI:
        code_text += u8"ANSI";
        break;
    case CodeType::UTF8:
        code_text += u8"UTF8";
        break;
    case CodeType::UTF8_BOM:
        code_text += u8"UTF8-BOM";
        break;
    case CodeType::UTF16:
        code_text += u8"UTF16";
        break;
    }
    m_pCodeLabel->setText(code_text);
}
